import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:untitled5/chat_page.dart';
import 'package:untitled5/discover_page.dart';
import 'package:untitled5/friends_page.dart';
import 'package:untitled5/me_page.dart';

class testhome extends StatefulWidget {
  const testhome({super.key});

  @override
  State<testhome> createState() => _testhomeState();
}

class MeAppBar {}

class _testhomeState extends State<testhome>
    with SingleTickerProviderStateMixin {
  PageController _pageController = PageController();
  int _currentIndex = 0;
  late TabController tabController;
  @override
  void initState() {
    super.initState();
    tabController = TabController(
      length: 3,
      vsync: this,
      animationDuration: const Duration(milliseconds: 1),
    );
    tabController.addListener(_handleTabChange);
  }

  void _handleTabChange() {
    setState(() {});
  }

  @override
  void dispose() {
    tabController.removeListener(_handleTabChange);
    _pageController.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        physics: ClampingScrollPhysics(),
        controller: _pageController,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        children: [
          testscreem(
            pageController: _pageController,
            tabController: tabController,
          ),
          const MePage()
        ],
      ),
      bottomNavigationBar: Container(
        height: 100,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            homebottomitem(
              img: 'images/friends.png',
              active: 'images/friendsgreen.png',
              select: tabController.index == 0 && _currentIndex == 0,
              name: '微信',
              // select:
              tap: () {
                _pageController.jumpToPage(0);
                tabController.index = 0;
              },
            ),
            homebottomitem(
              img: 'images/friends.png',
              active: 'images/friendsgreen.png',
              select: tabController.index == 1 && _currentIndex == 0,
              name: '通讯录',
              tap: () {
                _pageController.jumpToPage(0);
                tabController.index = 1;
              },
            ),
            homebottomitem(
              img: 'images/friends.png',
              active: 'images/friendsgreen.png',
              select: tabController.index == 2 && _currentIndex == 0,
              name: '发现',
              tap: () {
                _pageController.jumpToPage(0);
                tabController.index = 2;
              },
            ),
            homebottomitem(
              img: 'images/me.png',
              active: 'images/megreen.png',
              select: _currentIndex == 1,
              name: '我',
              tap: () {
                _pageController.jumpToPage(1);
              },
            ),
          ],
        ),
      ),
    );
  }
}

class homebottomitem extends StatelessWidget {
  final String img;
  final String active;
  final String name;
  final Function tap;
  const homebottomitem({
    super.key,
    this.select = false,
    required this.img,
    required this.active,
    required this.name,
    required this.tap,
  });
  final bool select;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Future.delayed(Duration.zero, () {
          tap(); // 调用传入的tap函数
        });
      },
      child: Column(
        children: [
          SizedBox(
            height: 30,
            child: select ? Image.asset(active) : Image.asset(img),
          ),
          Text(name)
        ],
      ),
    );
  }
}

class testscreem extends StatefulWidget {
  const testscreem(
      {super.key, required this.tabController, required this.pageController});
  final TabController tabController;
  final PageController pageController;
  @override
  State<testscreem> createState() => _testscreemState();
}

class _testscreemState extends State<testscreem>
    with SingleTickerProviderStateMixin {
  int count = 0;
  List titles = ["微信", "通讯录", "发现"];
  DragStartDetails? dragStartDetails;
  Drag? drag; //处理用户拖动手势的抽象类
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color.fromRGBO(237, 237, 237, 1.0),
        elevation: 0,
        title: Text(titles[widget.tabController.index]),
        bottom: (false)
            ? TabBar(
                // physics: NeverScrollableScrollPhysics(),
                controller: widget.tabController,
                tabs: [
                  Tab(text: 'Tab 1'),
                  Tab(text: 'Tab 2'),
                  Tab(text: 'Tab 3'),
                ],
              )
            : null,
      ),
      body: NotificationListener(
        onNotification: (notification) {
          if (notification is ScrollStartNotification) {
            dragStartDetails = notification.dragDetails;
          }
          if (notification is OverscrollNotification) {
            drag =
                widget.pageController.position.drag(dragStartDetails!, () {});
            drag?.update(notification.dragDetails!);
          }
          if (notification is ScrollEndNotification) {
            drag?.cancel();
          }
          return true;
        },
        child: TabBarView(
          // physics: ClampingScrollPhysics(),
          controller: widget.tabController, // 指定TabController
          children: const [
            ChatPage(),
            FriendsPage(),
            DiscoverPage(),
          ],
        ),
      ),
    );
  }
}
